import { ROOT, md5, toSuccess } from "@sker/core"
import { useData } from "@sker/runtime"
import { writeFileSync } from "fs"
import { ensureDirSync } from 'fs-extra'
import { join } from "path"
import { DependencyContainer } from "tsyringe"
import { z } from "zod"

export default async (injector: DependencyContainer) => {
    const root = injector.resolve(ROOT)
    const input = z.object({
        xml: z.string(),
        className: z.optional(z.string()),
        pkg: z.optional(z.string()),
        id: z.optional(z.string())
    })
    const body = input.parse(useData())
    const { xml, className, pkg, id } = body;
    const name = md5(`${xml}`)
    const dir = pkg ? pkg : 'default';
    ensureDirSync(join(root, `public/${dir}`))
    writeFileSync(join(root, `public/${dir}/${name}.html`), xml, 'utf-8')
    return toSuccess(true)
}